package com.example.dell.fastshopping.charge;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.charge.model.ChargeDetialModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by ${YSH} on 2019/4/26.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class ChargePagerFragment extends Fragment {
    @BindView(R.id.c_rv)
    RecyclerView rv;

    private Unbinder bind;
    private ChargeDetialModel model;
    private Context mContex;

    public static Fragment newInstance() {
        return new ChargePagerFragment();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContex = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_charge_detial, container, false);
        bind = ButterKnife.bind(this, inflate);
        return inflate;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        model = ViewModelProviders.of(this).get(ChargeDetialModel.class);
        initView();
    }

    private void initView() {
//        model.getChargeList().observe(
//                this, new Observer<ChargeEntity>() {
//                    @Override
//                    public void onChanged(@Nullable ChargeEntity chargeEntity) {
//
//                    }
//                }
//        );
        rv.setLayoutManager(new LinearLayoutManager(mContex));
        rv.setAdapter(new MyAdapter());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }

    private class MyAdapter extends RecyclerView.Adapter<MyHodel> {
        @NonNull
        @Override
        public MyHodel onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View inflate = LayoutInflater.from(mContex).inflate(R.layout.item_charge_detial, viewGroup, false);
            MyHodel myHodel = new MyHodel(inflate);
            return myHodel;
        }

        @Override
        public void onBindViewHolder(@NonNull MyHodel myHodel, int i) {
            myHodel.mItemView.setOnClickListener(myHodel);
            myHodel.buyButton.setOnClickListener(myHodel);
        }

        @Override
        public int getItemCount() {
            return 3;
        }
    }

    private class MyHodel extends RecyclerView.ViewHolder implements View.OnClickListener {

        public final View buyButton;
        public View mItemView;

        public MyHodel(@NonNull View itemView) {
            super(itemView);
            this.mItemView = itemView;
            this.buyButton = itemView.findViewById(R.id.buy);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.item:
                    toDetialPager();
                    break;
                case R.id.buy:
                    toSoldPager();
                    break;
            }

        }
    }

    private void toDetialPager() {
        Intent intent = new Intent(mContex, ChargeDetialActivity.class);
        startActivity(intent);
    }

    private void toSoldPager() {
        Intent intent = new Intent(mContex, IntegralSoldActivity.class);
        startActivity(intent);
    }
}
